package viroyal.com.base.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.just.agentweb.AgentWeb;
import com.just.agentweb.AgentWebConfig;
import com.mingle.widget.LoadingView;

import viroyal.com.base.R;


/**
 * Time:2020/5/13
 * Author:bichao
 * description:
 */
public abstract class BaseFragment extends Fragment {

//  private AgentWeb mAgentWeb;
//
//  protected void loadUrl(LinearLayout layout, String url) {
//    mAgentWeb = AgentWeb.with(this)
//        .setAgentWebParent(layout, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
//        .useDefaultIndicator()
//        .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
//        .setWebChromeClient(new MyWebChromeClient())
//        .setMainFrameErrorView(R.layout.agentweb_error_page, -1)
//        .interceptUnkownUrl()
//        .createAgentWeb()
//        .ready()
//        .go(url);
//
//    //初始化与JS交互
////    mAgentWeb.getJsInterfaceHolder().addJavaObject("Android", new AndroidInterfaceWeb(mAgentWeb, getActivity()));
//  }


  @Override
  public void onDestroyView() {
    AgentWebConfig.clearDiskCache(this.getContext());
    super.onDestroyView();
    isInit = false;
    isLoad = false;
  }


  /**
   * 视图是否已经初初始化
   */
  protected boolean isInit = false;
  protected boolean isLoad = false;
  protected final String TAG = "LazyLoadFragment";
  private View view;

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(setContentView(), container, false);
    isInit = true;
    /**初始化的时候去加载数据**/
    isCanLoadData();
    return view;
  }

  /**
   * 视图是否已经对用户可见，系统的方法
   */
  @Override
  public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    isCanLoadData();
  }

  /**
   * 是否可以加载数据
   * 可以加载数据的条件：
   * 1.视图已经初始化
   * 2.视图对用户可见
   */
  private void isCanLoadData() {
    if (!isInit) {
      return;
    }

    if (getUserVisibleHint()) {
      lazyLoad();
      isLoad = true;
    } else {
      if (isLoad) {
        stopLoad();
      }
    }
  }

  protected void showToast(String message) {
    if (!TextUtils.isEmpty(message)) {
      Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
    }

  }

  /**
   * 设置Fragment要显示的布局
   *
   * @return 布局的layoutId
   */
  protected abstract int setContentView();

  /**
   * 获取设置的布局
   *
   * @return
   */
  protected View getContentView() {
    return view;
  }

  /**
   * 找出对应的控件
   *
   * @param id
   * @param <T>
   * @return
   */
  protected <T extends View> T findViewById(int id) {

    return (T) getContentView().findViewById(id);
  }

  /**
   * 当视图初始化并且对用户可见的时候去真正的加载数据
   */
  protected abstract void lazyLoad();

  /**
   * 当视图已经对用户不可见并且加载过数据，如果需要在切换到其他页面时停止加载数据，可以调用此方法
   */
  protected void stopLoad() {
  }

  private class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
      super.onProgressChanged(view, newProgress);
      if (newProgress == 100) {
        LoadingView loadView = findViewById(R.id.loadView);
        loadView.setVisibility(View.GONE);
      }
    }
  }

}
